﻿using System.Net;
using System.Net.Sockets;
using System.Web;

namespace Uranus.Common
{
    public class VisitCommon
    {
        //服务器ip
        private static string _serverIp;
        //服务器名
        private static string _serverName;
        //客户端主浏览器版本号
        public string MajorVersion { get; set; }
        //客户端完整呢版本号
        public string Version { get; set; }
        //客户端使用平台名字
        public string Platform { get; set; }
        //客户端主机IP
        public string UserHostAddress { get; set; }
        //当前URL
        public string Url { get; set; }
        //客户端上次请求URL
        public string UrlReferrer { get; set; }
        //客户端浏览器的原始用户代理信息
        public string UserAgent { get; set; }
        //客户端DNS名称
        public string UserHostName { get; set; }
        //分辨率
        public string resolution { get; set; }
        ////浏览器的基本信息
        //public string browserInfo { get; set; }
        //浏览器型号
        public string Browsertype { get; set; }
        //浏览器名称
        public string BrowserName { get; set; }
        //浏览器宽度
        public int BrowserWidth { get; set; }
        //是否是移动设备
        public bool IsMobile { get; set; }

        public bool IsWx { get; set; }
        /// <summary>
        /// 设置request
        /// </summary>
        /// <param name="httpRequest"></param>
        public void SetVisit(HttpRequest httpRequest)
        {
            if (httpRequest != null)
            {
                MajorVersion = httpRequest.Browser.MajorVersion.ToString();
                Version = httpRequest.Browser.Version;
                Platform = httpRequest.Browser.Platform;
                UserHostAddress = httpRequest.ServerVariables["HTTP_X_REAL_IP"] != null ? httpRequest.ServerVariables["HTTP_X_REAL_IP"].ToString() : httpRequest["HTTP_X_FORWARDED_FOR"] == null ? httpRequest.UserHostAddress : httpRequest["HTTP_X_FORWARDED_FOR"].ToString();
                if (UserHostAddress != null && UserHostAddress.Contains(","))
                {
                    UserHostAddress = UserHostAddress.Split(',')[0];
                }
                Url = httpRequest.Url.ToString();
                UrlReferrer = httpRequest.UrlReferrer == null ? "" : httpRequest.UrlReferrer.ToString();
                UserAgent = httpRequest.UserAgent;
                UserHostName = httpRequest.UserHostName;
                HttpBrowserCapabilities bc = httpRequest.Browser;
                BrowserWidth = bc.ScreenPixelsWidth;
                resolution = httpRequest.Form["WidthPixel"] + "×" + httpRequest.Form["HeightPixel"];
                Browsertype = bc.Type;
                BrowserName = bc.Browser;
                IsMobile = false;
                UserAgent = httpRequest.UserAgent;
                IsWx = false;
                if (UserAgent != null)
                {
                    UserAgent = UserAgent.ToLower();
                    if (httpRequest.Browser.IsMobileDevice || UserAgent.Contains("iphone") ||
                        UserAgent.Contains("blackberry") || UserAgent.Contains("mobile") ||
                        UserAgent.Contains("windows ce") || UserAgent.Contains("opera mini") ||
                        UserAgent.Contains("palm") || UserAgent.Contains("android"))
                    {
                        IsMobile = true;
                    }
                    if (UserAgent.Contains("ipad"))
                    {
                        IsMobile = false;
                    }
                    if (UserAgent.Contains("micromessenger"))
                    {
                        IsWx = true;
                    }
                }
            }

        }
        /// <summary>
        /// 设置request
        /// </summary>
        /// <param name="httpRequest"></param>
        public void SetVisit(HttpRequestBase httpRequest)
        {
            if (httpRequest != null)
            {
                MajorVersion = httpRequest.Browser.MajorVersion.ToString();
                Version = httpRequest.Browser.Version;
                Platform = httpRequest.Browser.Platform;
                UserHostAddress = httpRequest.ServerVariables["HTTP_X_REAL_IP"] != null ? httpRequest.ServerVariables["HTTP_X_REAL_IP"].ToString() : httpRequest["HTTP_X_FORWARDED_FOR"] == null ? httpRequest.UserHostAddress : httpRequest["HTTP_X_FORWARDED_FOR"].ToString();
                if (UserHostAddress != null && UserHostAddress.Contains(","))
                {
                    UserHostAddress = UserHostAddress.Split(',')[0];
                }
                Url = httpRequest.Url.ToString();
                UrlReferrer = httpRequest.UrlReferrer != null ? httpRequest.UrlReferrer.ToString() : "";
                UserAgent = httpRequest.UserAgent;
                UserHostName = httpRequest.UserHostName;
                HttpBrowserCapabilitiesBase bc = httpRequest.Browser;
                resolution = httpRequest["WidthPixel"] + "×" + httpRequest["HeightPixel"];
                Browsertype = bc.Type;
                BrowserWidth = bc.ScreenPixelsWidth;
                BrowserName = bc.Browser;
                IsMobile = false;
                IsWx = false;
                if (UserAgent != null)
                {
                    UserAgent = UserAgent.ToLower();
                    if (httpRequest.Browser.IsMobileDevice || UserAgent.Contains("iphone") ||
                        UserAgent.Contains("blackberry") || UserAgent.Contains("mobile") ||
                        UserAgent.Contains("windows ce") || UserAgent.Contains("opera mini") ||
                        UserAgent.Contains("palm") || UserAgent.Contains("android"))
                    {
                        IsMobile = true;
                    }
                    if (UserAgent.Contains("ipad"))
                    {
                        IsMobile = false;
                    } if (UserAgent.Contains("micromessenger"))
                    {
                        IsWx = true;
                    }
                }
            }

        }

        public static string GetIp()
        {
            if (string.IsNullOrEmpty(_serverIp))
            {
                foreach (IPAddress c in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
                {
                    if (c.AddressFamily == AddressFamily.InterNetwork)
                    {
                        _serverIp = c.ToString();
                        break;
                    }
                }
                if (string.IsNullOrEmpty(_serverIp)) _serverIp = "无";
            }
            return _serverIp;
        }

        public static string GetServiceName()
        {
            if (string.IsNullOrEmpty(_serverName))
            {
                _serverName = Dns.GetHostName();
            }
            return _serverName;
        }
        /// <summary>
        /// 是否可正常登陆
        /// </summary>
        /// <param name="httpContext">响应报文头</param>
        /// <returns></returns>
        public static VisitDTO Manage(HttpContext httpContext)
        {
            VisitCommon info = new VisitCommon();
            info.SetVisit(httpContext.Request);
            switch (info.Browsertype)
            {
                case "IE5.5":
                case "IE6": return VisitDTO.Notaccess;
                case "IE7":
                    return VisitDTO.Poraccess;
                case "IE8":
                case "IE9":
                case "IE10":
                case "IE11":
                    return VisitDTO.Normal;
                case "Chrome13":
                case "Chrome14": return VisitDTO.Notaccess;
                case "Chrome15":
                case "Chrome16":
                case "Chrome17":
                case "Chrome18":
                case "Chrome19":
                case "Chrome20":
                case "Chrome21":
                case "Chrome22":
                case "Chrome23":
                case "Chrome24":
                case "Chrome25":
                case "Chrome26":
                case "Chrome27":
                case "Chrome28":
                case "Chrome29":
                case "Chrome30":
                case "Chrome31":
                case "Chrome32":
                case "Chrome33":
                case "Chrome34":
                    return VisitDTO.Normal;
                default:
                    {
                        return VisitDTO.Normal;
                    }
            }
        }
        /// <summary>
        /// 是否可正常登陆
        /// </summary>
        /// <param name="httpContext">响应报文头</param>
        /// <returns></returns>
        public static VisitDTO Manage(HttpContextBase httpContextBase)
        {
            VisitCommon info = new VisitCommon();
            info.SetVisit(httpContextBase.Request);
            switch (info.Browsertype)
            {
                case "IE5.5":
                case "IE6": return VisitDTO.Notaccess;
                case "IE7": return VisitDTO.Poraccess;
                case "IE8":
                //return VisitDTO.Notaccess;
                case "IE9":
                case "IE10":
                case "IE11":
                    return VisitDTO.Normal;
                case "Chrome13":
                case "Chrome14": return VisitDTO.Notaccess;
                case "Chrome15":
                case "Chrome16":
                case "Chrome17":
                case "Chrome18":
                case "Chrome19":
                case "Chrome20":
                case "Chrome21":
                case "Chrome22":
                case "Chrome23":
                case "Chrome24":
                case "Chrome25":
                case "Chrome26":
                case "Chrome27":
                case "Chrome28":
                case "Chrome29":
                case "Chrome30":
                case "Chrome31":
                case "Chrome32":
                case "Chrome33":
                case "Chrome34":
                    return VisitDTO.Normal;
                default:
                    {
                        return VisitDTO.Normal;
                    }
            }
        }

        public string GetAllVisitInfo()
        {
            return
                string.Format(
                    "MajorVersion:{0} Version:{1} Platform:{2} UserHostAddress:{3} UserAgent:{4} UserHostName:{5} resolution:{6} Browsertype:{7} BrowserName:{8} IsMobile:{9} IsWx:{10}",
                    MajorVersion, Version, Platform, UserHostAddress, UserAgent, UserHostName, resolution, Browsertype,
                    BrowserName, IsMobile, IsWx);
        }


    }

    public enum VisitDTO
    {
        //正常访问
        Normal,
        //能正常访问一些
        Poraccess,
        //不能正常访问 IE6
        Notaccess
    }

}